﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using AGDN;

namespace Sudum
{
    //this class handles all the game collision 
    //between bullets and enemies 
    //bullets and player ship
    //player ship and emeies

    class CollisionManager
    {

        const float bulletRadius = 2.0f;
        const float enemyRadius  = 0.5f;
        public void Update()
        {
            //get the enemies and bullet list to check collision between them
            List<Enemy> enemies = EnemyManager.Me.GetEnemies();
            List<Bullet> bullets = BulletManager.Me.GetBullets();

            for (int i = 0;i < enemies.Count;i++)
            {
                foreach (Bullet bullet in bullets)
                {
                    if (bullet.GetSource() == BulletSource.Player)
                    {
                        if (CheckCollision(enemies[i], bullet))
                        {
                            EnemyManager.Me.Remove(i);
                            BulletManager.Me.RemoveBullet(bullet);
                            break;
                        }
                    }
                }
            }

        }
        bool CheckCollision(Enemy enemy,Bullet bullet)
        {
            //create a sphere for every enemy and bullet and check collision
            BoundingSphere enemySphere = new BoundingSphere(enemy.GetPos(), enemyRadius);
            BoundingSphere bulletSphere = new BoundingSphere(bullet.GetPosition(), bulletRadius);

            if (enemySphere.Intersects(bulletSphere))
            {
                return true;
            }

            return false;
        }
    }
}
